package org.xs.big_event.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.xs.big_event.dao.aeticle.Article;
import org.xs.big_event.dao.aeticle.ArticleReponsitory;
import org.xs.big_event.dao.PageResult;
import org.xs.big_event.dao.pageRequest;
import org.xs.big_event.service.IArticleService;
import org.xs.big_event.utils.R;
import org.xs.big_event.utils.ResponseEnum;
import org.xs.big_event.utils.ThreadLocatUtil;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleService implements IArticleService {
    ArticleReponsitory articleReponsitory;
    //PageResult pageResult;
    @Autowired
    public ArticleService(
            ArticleReponsitory articleRepons) {
        this.articleReponsitory = articleRepons;
    }

    /*
    添加文章
      * @param article
      * @return ok
     */
    @Override
    public R add(Article article){
        //校验参数
        if (article.getTitle()==null||article.getTitle().equals("")) {
            return R.error(ResponseEnum.PARAM_ERROR);
        }
       // 获取当前用户ID
       Integer userId=Integer.valueOf(ThreadLocatUtil.get());
       article.setCreate_user(userId);
       article.setCreate_time(LocalDateTime.now());
       article.setUpdate_time(LocalDateTime.now());
       articleReponsitory.save(article);
       return R.ok();
    }
    /*
    * @Description: 文章列表(分页查询)
    * @Param: [pageRequest]
    * @return: org.xs.big_event.utils.R
    * 1. 参数校验与默认值处理
    * 2. 创建 Pageable 对象
    * 3.获取用户Id
    * 4. 调用 Repository 执行分页查询
    * 5. 封装 PageResult 对象
    * 6. 返回结果
     */
    @Override
    public R list(pageRequest pageRequest) {
        // 1. 参数校验与默认值处理
        int currentPage = (pageRequest.getPageNum() != null && pageRequest.getPageNum() > 0) ? pageRequest.getPageNum() : 1;
        int currentPageSize = (pageRequest.getPageSize() != null && pageRequest.getPageSize() > 0) ? pageRequest.getPageSize() : 3;
        // 2. 创建 Pageable 对象
        // JPA的页码是从0开始的，所以需要减1
        // 通常建议创建一个Sort对象来保证查询结果的稳定性
        Sort sort = Sort.by(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(currentPage - 1, currentPageSize, sort);
        //3.获取用户Id
        Integer userId = Integer.valueOf(ThreadLocatUtil.get());
        // 4. 调用 Repository 执行分页查询
        Page<Article> page = articleReponsitory.findListByUser(
                userId,
                pageRequest.getCateGoryId(),
                pageRequest.getStats(),
                pageable);
        PageResult pageResult = new PageResult();
        //5. 封装 PageResult 对象
        // 设置总页数
        pageResult.setTotalPage(page.getTotalPages());
        // 设置总记录数
        pageResult.setTotalCount(page.getTotalElements());
        // 设置当前页数据
        pageResult.setTotal(page.getSize());
        // 设置当前页数据
        pageResult.setItems(page.getContent());
        return R.ok(pageResult);
    }

}
